/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui.tree;

import javax.swing.Action;
import javax.swing.GroupLayout;
import javax.swing.JPanel;

import com.pearreview.util.review.Aspect;

public class BottomControlPanel extends JPanel {

	private static final long serialVersionUID = -7561392065145299942L;
	
	private GroupLayout layout;
	private ListControlPanel controlPanel;
	private ListAspectForm aspectForm;
	private ListCategoryForm categoryForm;
	private JPanel placeHolder;
	
	private Action openCatalogManager;
	private Action addCategoryAction; 
	private Action addAspectAction;
	private Action createCatalogAction;
	private Action refreshAction;
	private Action saveAction;
	
	public BottomControlPanel(
			Action openCatalogManager, 
			Action addCategoryAction, 
			Action addAspectAction,
			Action saveAsCatalogAction,
			Action refreshAction,
			Action saveAction) {
		
		super();
		
		this.openCatalogManager = openCatalogManager;
		this.addCategoryAction = addCategoryAction; 
		this.addAspectAction = addAspectAction;
		this.createCatalogAction = saveAsCatalogAction;
		this.refreshAction = refreshAction;
		this.saveAction = saveAction;
		
		initComponents();
	}
	
	private void initComponents() {
		setOpaque(false);
		
		controlPanel = new ListControlPanel(
				openCatalogManager,
				addCategoryAction,
				addAspectAction,
				createCatalogAction,
				refreshAction);
		aspectForm = new ListAspectForm(saveAction);
		categoryForm = new ListCategoryForm(saveAction);
		placeHolder = new JPanel();
		
		placeHolder.setOpaque(false);
		
		layout = new GroupLayout(this);
		setLayout(layout);
		
		initForm(placeHolder);
	}
	
	private void initForm(JPanel panel) {
		removeAll();
		layout.setHorizontalGroup(
				layout.createSequentialGroup()
					.addComponent(controlPanel)
					.addComponent(panel)
		);
		
		layout.setVerticalGroup(
				layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(
						GroupLayout.Alignment.BASELINE)
					.addComponent(controlPanel)
					.addComponent(panel, 
							aspectForm.getMinimumSize().height + 20, 
							aspectForm.getPreferredSize().height + 20, 
							aspectForm.getMaximumSize().height + 20))
		);
		
		validate();
	}
	
	public void showAspectForm(Aspect aspect) {
		setAspect(aspect);
		this.initForm(aspectForm);
	}
	
	public void showAspectForm(Aspect aspect, String[] categories) {
		setAspect(aspect, categories);
		this.initForm(aspectForm);
	}
	
	public void showCategoryForm(String text) {
		categoryForm.setCategory(text);
		this.initForm(categoryForm);
	}
	
	public void showNoForm() {
		this.initForm(placeHolder);
	}

	public Aspect getAspect() {
		return aspectForm.getAspect();
	}
	
	public void setAspect(Aspect aspect) {
		aspectForm.setAspect(aspect);
	}
	
	public void setAspect(Aspect aspect, String[] categories) {
		aspectForm.setAspect(aspect, categories);
	}
	
	public String getCategory() {
		return categoryForm.getCategory();
	}
	
	public void setCategory(String category) {
		categoryForm.setCategory(category);
	}
}
